package com.plian.system.service.sys.orgzation.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.plian.Tools.CollectionUtil;
import com.plian.Tools.StringPool;
import com.plian.system.config.exception.MyRuntimeException;
import com.plian.system.constant.sys.SystemConstant;
import com.plian.system.entity.sys.ServiceOrgFormType;
import com.plian.system.mapper.sys.ServiceOrgFormTypeMapper;
import com.plian.system.model.ObjResult;
import com.plian.system.node.ForestNodeMerger;
import com.plian.system.service.sys.orgzation.IServiceOrgFormTypeService;
import com.plian.system.vo.sys.BaseServiceOrgVO;
import com.plian.system.vo.sys.ServiceOrgFormTypeVO;
import com.plian.system.wrapper.sys.ServiceOrgFormTypeWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
@AllArgsConstructor
public class ServiceOrgFormTypeServiceImpl extends ServiceImpl<ServiceOrgFormTypeMapper, ServiceOrgFormType> implements IServiceOrgFormTypeService {

    private ServiceOrgFormTypeWrapper serviceOrgFormTypeWrapper;

    private static final Integer MODEL_TYPE = 3;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public boolean saveList(BaseServiceOrgVO baseServiceOrgVO) {
        if (baseServiceOrgVO.getId() == null){
            throw new MyRuntimeException(new ObjResult("业务组织ID 为null！"));
        }
        boolean result = update(Wrappers.<ServiceOrgFormType>lambdaUpdate()
                .eq(ServiceOrgFormType::getBaseServiceOrgId, baseServiceOrgVO.getId())
                .set(ServiceOrgFormType::getBaseServiceOrgId, null));
        if (CollectionUtil.isEmpty(baseServiceOrgVO.getCheckedFormTypeList())){
            return result;
        }
        return  update(Wrappers.<ServiceOrgFormType>lambdaUpdate()
                .eq(ServiceOrgFormType::getType, MODEL_TYPE)
                .in(ServiceOrgFormType::getId, baseServiceOrgVO.getCheckedFormTypeList())
                .set(ServiceOrgFormType::getBaseServiceOrgId, baseServiceOrgVO.getId()));
    }

//    @Override TODO DEL
//    public String checkList(BaseServiceOrgVO baseServiceOrgVO) {
//        if (baseServiceOrgVO.getId() == null || CollectionUtil.isEmpty(baseServiceOrgVO.getCheckedFormTypeList())){
//            return "";
//        }
//        List<ServiceOrgFormType> serviceOrgFormTypeList = list(Wrappers.<ServiceOrgFormType>lambdaQuery()
//                .isNotNull(ServiceOrgFormType::getBaseServiceOrgId)
//                .eq(ServiceOrgFormType::getType, MODEL_TYPE)
//                .ne(ServiceOrgFormType::getBaseServiceOrgId, baseServiceOrgVO.getId())
//                .in(ServiceOrgFormType::getId, baseServiceOrgVO.getCheckedFormTypeList()));
//
//        if (CollectionUtil.isEmpty(serviceOrgFormTypeList)){
//            return "";
//        }
//        return serviceOrgFormTypeList.stream()
//                .map(ServiceOrgFormType::getName)
//                .collect(Collectors.joining(StringPool.COMMA));
//    }

    @Override
    public List<ServiceOrgFormTypeVO> formTypeTree(Long baseServiceOrgId) {
        List<ServiceOrgFormTypeVO> list = serviceOrgFormTypeWrapper.entityToVO(list());
        if (CollectionUtil.isEmpty(list)){
            return new ArrayList<>();
        }
        for (ServiceOrgFormTypeVO typeVO : list){
            typeVO.setIsChecked(SystemConstant.NO);
            if (baseServiceOrgId.equals(typeVO.getBaseServiceOrgId())){
                typeVO.setIsChecked(SystemConstant.YES);
            }
        }
        return ForestNodeMerger.merge(list);
    }

    @Override
    public String getCheckName(Long baseServiceOrgId) {
        if (baseServiceOrgId == null){
            return "";
        }
        List<ServiceOrgFormType> typeList = list(Wrappers.<ServiceOrgFormType>lambdaQuery()
                .eq(ServiceOrgFormType::getType, MODEL_TYPE)
                .eq(ServiceOrgFormType::getBaseServiceOrgId, baseServiceOrgId));
        if (CollectionUtil.isEmpty(typeList)){
            return "";
        }
        return typeList.stream()
                .map(ServiceOrgFormType::getName)
                .collect(Collectors.joining(StringPool.COMMA));
    }
}
